<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of propierties_controller
 *
 * @author 250-1-405
 */
class PropertiesController extends AppController {

    //put your code here
    var $name = "Properties";

    public function index() {
        if ($this->Session->read('proyect_id') != "") {
            $proyect_id = $this->Session->read('proyect_id');
            $this->set('proyect_id', $proyect_id);
            $this->paginate = array('Property' => array('recursive' => -1, 'maxLimit' => 500, 'limit' => 50, 'fields' => array('Property.id', 'Property.nombre', 'Property.matricula', 'Property.cedulaCatastral', 'Property.area', 'Property.area_permuta')));
            $this->set('Properties', $this->paginate(array('Property.proyect_id' => $proyect_id)));
        } else {
            $this->Session->setFlash('No ha seleccionado permuta', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    public function index_technical_visit() {
        if ($this->Session->read('proyect_id') != "") {
            $proyect_id = $this->Session->read('proyect_id');
            $this->set('proyect_id', $proyect_id);
            $this->paginate = array('Property' => array('recursive' => -1, 'maxLimit' => 500, 'limit' => 50, 'fields' => array('Property.id', 'Property.nombre', 'Property.matricula', 'Property.cedulaCatastral')));
            $this->set('Properties', $this->paginate(array('Property.proyect_id' => $proyect_id)));
        } else {
            $this->Session->setFlash('No ha seleccionado permuta', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    public function add($proyect_id) {
        $this->set('proyect_id', $proyect_id);
        $this->set('departaments', $this->Property->City->Departament->find('list', array('fields' => array('Departament.id', 'Departament.nombre'))));
        if (empty($this->data)) {
            
        } else {
            if ($this->Property->save($this->data)) {
                $this->Session->setFlash('Registro Adicionado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Properties', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error Guardando datos');
            }
        }
    }

    public function edit($id) {
        $this->Property->recursive = -1;
        $this->set('departaments', $this->Property->City->Departament->find('list', array('fields' => array('Departament.id', 'Departament.nombre'))));
        $this->set('cities', $this->Property->City->find('list', array(
                    'order' => 'nombre ASC',
                    'fields' => array('City.id', 'City.nombre')
                        )
                ));


        if (empty($this->data)) {

            $this->data = $this->Property->find('first', array('conditions' => array('Property.id' => $id), 'joins' => array(array('table' => 'cities', 'alias' => 'City', 'type' => 'left', 'conditions' => 'City.id=Property.city_id'), array('table' => 'departaments', 'alias' => 'Departament', 'type' => 'left', 'conditions' => 'City.departament_id=Departament.id')), 'fields' => array('Property.*', 'Departament.id')));
        } else {

            if ($this->Property->save($this->data)) {
                $this->Session->setFlash('Registro editado correctamente', 'flash_custom');
                $this->redirect(array('controller' => 'Properties', 'action' => 'index'));
            } else {
                $this->Session->setFlash('Error editando datos');
            }
        }
    }

    public function title_index() {
        if ($this->Session->read('proyect_id') != "") {
            $proyect_id = $this->Session->read('proyect_id');
            $this->paginate = array('Property' => array('maxLimit' => 500, 'limit' => 50, 'fields' => array('Property.id', 'Property.nombre', 'Property.matricula', 'Property.cedulaCatastral', 'Property.id')));
            $this->set('Properties', $this->paginate(array('Property.proyect_id' => $proyect_id)));
        } else {
            $this->Session->setFlash('No ha seleccionado permuta', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }
    public function evaluation_index() {
        if ($this->Session->read('proyect_id') != "") {
            $proyect_id = $this->Session->read('proyect_id');
            $this->paginate = array('Property' => array('maxLimit' => 500, 'limit' => 50, 'fields' => array('Property.id', 'Property.nombre', 'Property.matricula', 'Property.cedulaCatastral', 'Property.id')));
            $this->set('Properties', $this->paginate(array('Property.proyect_id' => $proyect_id)));
        } else {
            $this->Session->setFlash('No ha seleccionado permuta', 'flash_custom');
            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
        }
    }

    function select() {

        $this->set('cities', $this->Property->City->find('list', array(
                    'order' => 'nombre ASC',
                    'conditions' => array('City.departament_id' => $this->data['Property']['departament_id']),
                    'fields' => array('City.id', 'City.nombre')
                        )
                ));
    }

}

?>